#include "wps_process_dock.h"
#include "mainwindow.h"

WPSProcessDock::WPSProcessDock(MainWindow* aParent) : main_window_(aParent),MDockAncestor(aParent)
{
    retranslateUi();
    wps_edit_widget_ = new QWidget(this);
    vbox_layout_ = new QVBoxLayout(wps_edit_widget_);
    wps_edit_widget_->setLayout(vbox_layout_);

    seq_num_label_ = new QLabel();
    seq_num_label_->setText("序列号");

    lon_label_ = new QLabel();
    lon_label_->setText("经度");

    lat_label_ = new QLabel();
    lat_label_->setText("纬度");

    speed_limit_label_ = new QLabel();
    speed_limit_label_->setText("速度限制");

    road_type_label_ = new QLabel();
    road_type_label_->setText("路型");

    left_boundary_label_ = new QLabel();
    left_boundary_label_->setText("左边界");

    right_boundary_label_ = new QLabel();
    right_boundary_label_->setText("右边界");

    seq_num_line_text_ = new QLineEdit("seq_num");
    seq_num_line_text_->setObjectName("seq_num");
    QValidator *validator_1=new QIntValidator(0,999,wps_edit_widget_);
    seq_num_line_text_->setValidator(validator_1);

    lon_line_text_ = new QLineEdit("lon");
    lon_line_text_->setObjectName("lon");
    QValidator *validator_2=new QDoubleValidator(0,999,7);
    lon_line_text_->setValidator(validator_2);
    lon_line_text_->setReadOnly(true);


    lat_line_text_ = new QLineEdit("lat");
    lat_line_text_->setObjectName("lat");
    QValidator *validator_3=new QDoubleValidator(0,999,7);
    lat_line_text_->setValidator(validator_3);
    lat_line_text_->setReadOnly(true);

    speed_limit_line_text_ = new QLineEdit("speed_limit");
    speed_limit_line_text_->setObjectName("speed_limit");
    QValidator *validator_4=new QDoubleValidator(0,999,2);
    speed_limit_line_text_->setValidator(validator_4);

    road_type_line_text_ = new QLineEdit("road_type");
    road_type_line_text_->setObjectName("road_type");
    QValidator *validator_5=new QIntValidator(0,999,wps_edit_widget_);
    road_type_line_text_->setValidator(validator_5);

    left_boundary_line_text_ = new QLineEdit("left_bound");
    left_boundary_line_text_->setObjectName("left_bound");
    QValidator *validator_6=new QDoubleValidator(-999,0,2);
    left_boundary_line_text_->setValidator(validator_6);

    right_boundary_line_text_ = new QLineEdit("right_bound");
    right_boundary_line_text_->setObjectName("right_bound");
    QValidator *validator_7=new QDoubleValidator(0,999,2);
    right_boundary_line_text_->setValidator(validator_7);

    button_layout_ = new QGridLayout(wps_edit_widget_);
    button_layout_->addWidget(seq_num_label_, 0, 0, 1, 1);
    button_layout_->addWidget(seq_num_line_text_, 0, 1, 1, 1);
    button_layout_->addWidget(lon_label_, 0, 2, 1, 1);
    button_layout_->addWidget(lon_line_text_, 0, 3, 1, 1);
    button_layout_->addWidget(lat_label_, 0, 4, 1, 1);
    button_layout_->addWidget(lat_line_text_, 0, 5, 1, 1);
    button_layout_->addWidget(speed_limit_label_, 0, 6, 1, 1);
    button_layout_->addWidget(speed_limit_line_text_, 0, 7, 1, 1);
    button_layout_->addWidget(road_type_label_, 0, 8, 1, 1);
    button_layout_->addWidget(road_type_line_text_, 0, 9, 1, 1);
    button_layout_->addWidget(left_boundary_label_, 0, 10, 1, 1);
    button_layout_->addWidget(left_boundary_line_text_, 0, 11, 1, 1);
    button_layout_->addWidget(right_boundary_label_, 0, 12, 1, 1);
    button_layout_->addWidget(right_boundary_line_text_, 0, 13, 1, 1);
    button_layout_->setMargin(0);
    vbox_layout_->addLayout(button_layout_);
    vbox_layout_->setMargin(0);
    this->setWidget(wps_edit_widget_);

    connect(seq_num_line_text_,SIGNAL(returnPressed()),this,SLOT(OnEditLineTriggered()));
    connect(lon_line_text_,SIGNAL(returnPressed()),this,SLOT(OnEditLineTriggered()));
    connect(lat_line_text_,SIGNAL(returnPressed()),this,SLOT(OnEditLineTriggered()));
    connect(speed_limit_line_text_,SIGNAL(returnPressed()),this,SLOT(OnEditLineTriggered()));
    connect(road_type_line_text_,SIGNAL(returnPressed()),this,SLOT(OnEditLineTriggered()));
    connect(left_boundary_line_text_,SIGNAL(returnPressed()),this,SLOT(OnEditLineTriggered()));
    connect(right_boundary_line_text_,SIGNAL(returnPressed()),this,SLOT(OnEditLineTriggered()));
}

void WPSProcessDock::OnEditLineTriggered()
{
    QLineEdit* action = qobject_cast<QLineEdit*>(sender());
    if (action) {
        QString actionName = action->objectName();
        int seq_num = std::stoi(seq_num_line_text_->text().toStdString());
        double lon = std::stod(lon_line_text_->text().toStdString());
        double lat = std::stod(lat_line_text_->text().toStdString());
        float speed_limit = std::stof(speed_limit_line_text_->text().toStdString());
        int road_type = std::stoi(road_type_line_text_->text().toStdString());
        float left_bound = std::stof(left_boundary_line_text_->text().toStdString());
        float right_bound = std::stof(right_boundary_line_text_->text().toStdString());
        printf("1[%d: %lf %lf]  %f\r\n",seq_num,lat,lon,left_bound);
        modify_point_.speed_limit = speed_limit;
        modify_point_.left_bound = left_bound;
        modify_point_.right_bound = right_bound;
        modify_point_.lon = lon;
        modify_point_.lat = lat;
        printf("2  %d[%d: %lf %lf]\r\n",set_of_map_info_.wps_points.size(),modify_point_.index,modify_point_.lat,modify_point_.lon);
        std::cout<<actionName.toStdString()<<" 1111size: "<<set_of_map_info_.wps_points.size()<<std::endl;
        auto it = std::find_if(set_of_map_info_.wps_points.begin(), set_of_map_info_.wps_points.end(), [&](const STRoadPoint& element) {
            if((fabs(modify_point_.global_x - element.global_x)<1e-8)&&(fabs(modify_point_.global_y - element.global_y)<1e-8))
            {
                printf("have find....\r\n");
                return true;
            }
            else
                return false;
        });

        if(it != set_of_map_info_.wps_points.end())
        {
            printf("have find [%d: %f %f]:[%d: %f %f]\r\n",it->index,it->lat,it->lon,modify_point_.index,modify_point_.lat,modify_point_.lon);
            *it = modify_point_;
        }
        if(seq_num != modify_point_.index)
        {
            if(seq_num >= set_of_map_info_.wps_points.size())
            {
                seq_num = set_of_map_info_.wps_points.size();
            }
            printf("sqe num %d,index %d\r\n",seq_num,modify_point_.index);
            std::iter_swap(set_of_map_info_.wps_points.begin() + seq_num, set_of_map_info_.wps_points.begin() + modify_point_.index);
        }
        std::cout<<actionName.toStdString()<<" size: "<<set_of_map_info_.wps_points.size()<<std::endl;
        emit EmitEditWPSPointSignal(modify_point_,set_of_map_info_);
    }
}

void WPSProcessDock::OnDisplayWPSPointInfoTriggered(const STRoadPoint &point,SetofMapInfo &set_of_map_info)
{
    if(point.index == -1)
        return;
    set_of_map_info_ = set_of_map_info;
    modify_point_ = point;

    printf("%d[%d: %lf %lf]\r\n",set_of_map_info_.wps_points.size(),modify_point_.index,modify_point_.lat,modify_point_.lon);

    seq_num_line_text_->setText(QString::fromStdString(to_string(point.index)));
    std::string str = vehicle::tools::formatFloat(point.lon,7);
    lon_line_text_->setText(QString::fromStdString(str));
    str = vehicle::tools::formatFloat(point.lat,7);
    lat_line_text_->setText(QString::fromStdString(str));
    str = vehicle::tools::formatFloat(point.speed_limit,2);
    speed_limit_line_text_->setText(QString::fromStdString(str));
    road_type_line_text_->setText(QString::fromStdString(to_string(2)));
    str = vehicle::tools::formatFloat(point.left_bound,2);
    left_boundary_line_text_->setText(QString::fromStdString(str));
    str = vehicle::tools::formatFloat(point.right_bound,2);
    right_boundary_line_text_->setText(QString::fromStdString(str));
}

void WPSProcessDock::OnStartRecordButtonClicked()
{

}

void WPSProcessDock::OnStopRecordButtonClicked()
{

}

void WPSProcessDock::retranslateUi() {
}


void WPSProcessDock::hideEvent(QHideEvent *anEvent) {
    QWidget::hideEvent(anEvent);
}

void WPSProcessDock::showEvent(QShowEvent *anEvent) {
  QWidget::showEvent(anEvent);
}

void WPSProcessDock::CheckBoxChecked(int state) {

}
